﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.DirectoryServices;
using Librerias;

namespace Aplicacion.Modelos
{
    public class Usuario
    {
        

        public string Path { get; set; }
        public string Username { get; set; }

        private static List<Usuario> lista;

        
        public static IList<Usuario> GetUsuariosAD()
        {
            lista = new List<Usuario>();

            ObjetoAd _ad = new ObjetoAd("LDAP://10.1.9.33/ou=USUARIOS,dc=tierradelfuego,dc=gov,dc=ar");
            
            using (var deSearch = new DirectorySearcher(_ad.DirectoryEntry))
            {
                deSearch.Filter = "(&(objectClass=user))";
                deSearch.SearchScope = SearchScope.Subtree;
                deSearch.PageSize = 1000;

                SearchResultCollection results = deSearch.FindAll();
                if (results.Count > 0)
                {
                    foreach (SearchResult item in results)
                    {
                        _ad.GetObjeto(item.Path);
                        lista.Add(new Usuario
                        {
                            Path = item.Path,
                            Username = _ad.SamAccountName
                        });
                        /*
                        yield return new Usuario{
                            Path = item.Path,
                            Username = _ad.SamAccountName
                        };*/
                    }
                }
            }
            return lista;
        }

        public static IList<Usuario> GetUsuariosInactivos()
        {
            lista = new List<Usuario>();
            ObjetoAd _ad = new ObjetoAd("LDAP://10.1.9.33/ou=USUARIOS,dc=tierradelfuego,dc=gov,dc=ar");
            using (var deSearch = new DirectorySearcher(_ad.DirectoryEntry))
            {
                deSearch.Filter = "(&(objectClass=user)(lastLogon=0))";
                deSearch.SearchScope = SearchScope.Subtree;
                deSearch.PageSize = 1000;

                SearchResultCollection results = deSearch.FindAll();
                if (results.Count > 0)
                {
                    foreach (SearchResult item in results)
                    {
                        _ad.GetObjeto(item.Path);
                        lista.Add(new Usuario
                        {
                            Path = item.Path,
                            Username = _ad.SamAccountName
                        });
                        /*
                        yield return new Usuario{
                            Path = item.Path,
                            Username = _ad.SamAccountName
                        };*/
                    }
                }
            }

            return lista;
        }

        public static IList<Usuario> GetUsuariosZIMBRA()
        {
            List<Usuario> lista = new List<Usuario>();
            using (DirectoryEntry _de = new DirectoryEntry(@"LDAP://10.1.9.191:389/ou=people,dc=tierradelfuego,dc=gov,dc=ar", "uid=zimbra,cn=admins,cn=zimbra", "_YlZfHQ8l",AuthenticationTypes.None))
            {
                //IEnumerable<SearchResult> listaZimbra = zimbra.getUsers();
                using (var deSearch = new DirectorySearcher(_de))
                {
                    deSearch.Filter = "(&(objectClass=zimbraAccount))";
                    deSearch.SearchScope = SearchScope.Subtree;
                    SearchResultCollection results = deSearch.FindAll();
                    if (results.Count > 0)
                        foreach (SearchResult item in results)
                        {
                            var tmp = new DirectoryEntry(item.Path, "uid=zimbra,cn=admins,cn=zimbra", "_YlZfHQ8l", AuthenticationTypes.None);
                            lista.Add(new Usuario
                            {
                                Path = item.Path,
                                Username = tmp.Properties["uid"].Value.ToString()
                            });
                            /*
                            yield return new Usuario
                            {
                                Path = item.Path,
                                Username = tmp.Properties["uid"].Value.ToString()
                            };*/
                        }
                }
            }

            return lista;
        }
    }
}